# Copyright (c) 2008-2009-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::Account;
use strict;
use utf8;
use Settings;
use server::Session;
use server::Response;
use storage::OriginalXML;

sub new 
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{'id'} = shift;
	$self->{'username'} = shift;
	$self->{'password'} = shift;
	$self->{'x'} = shift;
	$self->{'y'} = shift;
	$self->{Storage} = dcExplorer::storage::OriginalXML->new($Settings::DataPath."xml", $self->{'id'});
	$self->{Session} = dcExplorer::server::Session->new($self->{'username'}, $self->{'password'});
	$self->{Empire} = undef;
	$self->{Turns} = {};
	bless ($self, $class);
	return $self;
}

sub getTurnList
{
	my $self = shift;
	return $self->{Storage}->GetTurnList();
}

sub getLast
{
	my $self = shift;
	$self->{Empire} = $self->{Storage}->GetLast();
	$self->{Turns}->{$self->{Empire}->getProp('turn')} = $self->{Empire};
	return $self->{Empire};
}

sub getEmpire
{
	my $self = shift;
	$self->{Empire} = $self->{Storage}->GetLast() unless defined $self->{Empire};
	my $turn = $self->{Empire}->getProp('turn');
	if (defined($turn))
	{
		$self->{Turns}->{$turn} = $self->{Empire} 
		unless defined $self->{Turns}->{$turn};
	}
	return $self->{Empire};
}

sub getTurn
{
	my $self = shift;
	my $turn = shift;
	$self->{Turns}->{$turn} = $self->{Storage}->GetTurn($turn) unless defined $self->{Turns}->{$turn};
	$self->{Empire} = $self->{Turns}->{$turn};
	return $self->{Empire};
}

sub download
{
	my $self = shift;
	my $types = shift;
	$self->{Empire} = $self->{Storage}->Download($self->{Session}, $types);
	$self->{Turns}->{$self->{Empire}->getProp('turn')} = $self->{Empire};
	return $self->{Empire};
}

sub havePendingActions
{
	my $self = shift;
	return $self->{Empire}->commandQueue->size;
}

sub submitActions
{
	my $self = shift;
	my $xml = $self->{Empire}->commandQueue()->getXML;
	my $res = $self->{Session}->PostXML($xml);
	if ($res)
	{
		my $success = 1;
		my $response = dcExplorer::server::Response->new($res);
		for my $id (sort keys %{$response->{Results}})
		{
			my $result = $response->{Results}->{$id};
			print $id." ".
				($result->{'is-successful'} ? 
					"It works!".($result->{'return-id'} ? " Response id: ".$result->{'return-id'} : "") : 
					"Ooops, it failed... Error id: ".$result->{'error-id'}.". Reason: ".$result->{'error-text'})
			."\n" if $Settings::Debug;
			$success = 0 unless $response->{Results}->{$id}->{'is-successful'};
		}
		$self->{Empire}->commandQueue()->clear;
		return $success;
	}
	else
	{
		return undef;
	}
}

1;
